package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.IEmployeeBizService;
import com.kk.xhr.admin.controller.request.EducationExperienceAddRequest;
import com.kk.xhr.admin.controller.request.EducationExperienceModifyRequest;
import com.kk.xhr.admin.controller.request.EmployeeAddRequest;
import com.kk.xhr.admin.controller.request.EmployeeModifyRequest;
import com.kk.xhr.admin.controller.request.EmployeeQueryRequest;
import com.kk.xhr.admin.controller.request.JobExperienceAddRequest;
import com.kk.xhr.admin.controller.request.JobExperienceModifyRequest;
import com.kk.xhr.admin.controller.request.PrizeExperienceAddRequest;
import com.kk.xhr.admin.controller.request.PrizeExperienceModifyRequest;
import com.kk.xhr.admin.controller.request.ProjectExperienceAddRequest;
import com.kk.xhr.admin.controller.request.ProjectExperienceModifyRequest;
import com.kk.xhr.admin.controller.vo.EducationExperienceVO;
import com.kk.xhr.admin.controller.vo.EmployeeVO;
import com.kk.xhr.admin.controller.vo.JobExperienceVO;
import com.kk.xhr.admin.controller.vo.PrizeExperienceVO;
import com.kk.xhr.admin.controller.vo.ProjectExperienceVO;
import com.kk.xhr.common.ErrorCodeEnum;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import com.kk.xhr.web.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 员工履历管理控制器
 *
 * @author luokexiong
 * @version 1.0 2021/2/15
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "员工履历管理", tags = "员工履历管理")
@RequestMapping("/employee")
public class EmployeeController {
    private final Executor adminControllerBizExecutor;
    private final IEmployeeBizService employeeBizService;

    @ApiOperation(value = "添加员工履历")
    @PostMapping
    public CompletionStage<ApiResult<Long>> add(@RequestBody @Valid EmployeeAddRequest request) {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(employeeBizService.add(userId, request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加工作履历")
    @PostMapping("/job")
    public CompletionStage<ApiResult<Long>> addJob(@RequestBody @Valid JobExperienceAddRequest request) {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(employeeBizService.addJob(request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加获奖履历")
    @PostMapping("/prize")
    public CompletionStage<ApiResult<Long>> addPrize(@RequestBody @Valid PrizeExperienceAddRequest request) {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(employeeBizService.addPrize(request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加教育经历")
    @PostMapping("/edu")
    public CompletionStage<ApiResult<Long>> addEdu(@RequestBody @Valid EducationExperienceAddRequest request) {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(employeeBizService.addEducation(request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加项目履历")
    @PostMapping("/project")
    public CompletionStage<ApiResult<Long>> addProject(@RequestBody @Valid ProjectExperienceAddRequest request) {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(employeeBizService.addProject(request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "分页查询员工", notes = "管理员操作")
    @GetMapping
    @RequiresRoles(value = {"admin"})
    public CompletionStage<ApiResult<PageVO<EmployeeVO>>> queryPage(@Valid EmployeeQueryRequest request) {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(employeeBizService.queryPage(request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询员工个人信息")
    @GetMapping("/mine")
    public CompletionStage<ApiResult<EmployeeVO>> queryEmployee() {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(employeeBizService.queryEmployee(userId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询员工工作经历")
    @GetMapping("/job/{id:.+}")
    public CompletionStage<ApiResult<List<JobExperienceVO>>> queryJob(@PathVariable("id") Long employeeId) {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(employeeBizService.queryJob(employeeId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询员工获奖经历")
    @GetMapping("/prize/{id:.+}")
    public CompletionStage<ApiResult<List<PrizeExperienceVO>>> queryPrize(@PathVariable("id") Long employeeId) {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(employeeBizService.queryPrize(employeeId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询员工教育经历")
    @GetMapping("/edu/{id:.+}")
    public CompletionStage<ApiResult<List<EducationExperienceVO>>> queryEducation(@PathVariable("id") Long employeeId) {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(employeeBizService.queryEducation(employeeId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询员工项目经历")
    @GetMapping("/project/{id:.+}")
    public CompletionStage<ApiResult<List<ProjectExperienceVO>>> queryProject(@PathVariable("id") Long employeeId) {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(employeeBizService.queryProject(employeeId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改员工信息")
    @PutMapping
    public CompletionStage<ApiResult<Void>> modify(@RequestBody @Valid EmployeeModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            if (employeeBizService.modify(request)) {
                return ApiResult.success();
            }
            return ApiResult.failed(ErrorCodeEnum.RETRY);
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改员工工作经历信息")
    @PutMapping("/job")
    public CompletionStage<ApiResult<Void>> modifyJob(@RequestBody @Valid JobExperienceModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            if (employeeBizService.modifyJob(request)) {
                return ApiResult.success();
            }
            return ApiResult.failed(ErrorCodeEnum.RETRY);
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改员工获奖信息")
    @PutMapping("/prize")
    public CompletionStage<ApiResult<Void>> modifyPrize(@RequestBody @Valid PrizeExperienceModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            if (employeeBizService.modifyPrize(request)) {
                return ApiResult.success();
            }
            return ApiResult.failed(ErrorCodeEnum.RETRY);
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改员工教育经历信息")
    @PutMapping("/edu")
    public CompletionStage<ApiResult<Void>> modifyEducation(@RequestBody @Valid EducationExperienceModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            if (employeeBizService.modifyEducation(request)) {
                return ApiResult.success();
            }
            return ApiResult.failed(ErrorCodeEnum.RETRY);
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改员工项目经历信息")
    @PutMapping("/project")
    public CompletionStage<ApiResult<Void>> modifyProject(@RequestBody @Valid ProjectExperienceModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            if (employeeBizService.modifyProject(request)) {
                return ApiResult.success();
            }
            return ApiResult.failed(ErrorCodeEnum.RETRY);
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "删除员工工作经历")
    @DeleteMapping("/job/{id:.+}")
    public CompletionStage<ApiResult<Void>> removeJob(@PathVariable("id") Long id) {
        return CompletableFuture.supplyAsync(() -> {
            if (employeeBizService.removeJob(id)) {
                return ApiResult.success();
            }
            return ApiResult.failed(ErrorCodeEnum.RETRY);
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "删除员工获奖经历")
    @DeleteMapping("/prize/{id:.+}")
    public CompletionStage<ApiResult<Void>> removePrize(@PathVariable("id") Long id) {
        return CompletableFuture.supplyAsync(() -> {
            if (employeeBizService.removePrize(id)) {
                return ApiResult.success();
            }
            return ApiResult.failed(ErrorCodeEnum.RETRY);
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "删除员工教育经历")
    @DeleteMapping("/edu/{id:.+}")
    public CompletionStage<ApiResult<Void>> removeEducation(@PathVariable("id") Long id) {
        return CompletableFuture.supplyAsync(() -> {
            if (employeeBizService.removeEducation(id)) {
                return ApiResult.success();
            }
            return ApiResult.failed(ErrorCodeEnum.RETRY);
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "删除员工项目经历")
    @DeleteMapping("/project/{id:.+}")
    public CompletionStage<ApiResult<Void>> removeProject(@PathVariable("id") Long id) {
        return CompletableFuture.supplyAsync(() -> {
            if (employeeBizService.removeProject(id)) {
                return ApiResult.success();
            }
            return ApiResult.failed(ErrorCodeEnum.RETRY);
        }, adminControllerBizExecutor);
    }
}
